import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/Table';
import { serverList } from '@apps/ant-bus/src/views/business/goods/server/api';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { RenderType } from '@pkg/setting';

export const columns: BasicColumn[] = [
  {
    title: '卡种',
    dataIndex: 'goodsName',
    width: 150,
  },
  {
    title: '权益名称',
    dataIndex: 'serverName',
    width: 150,
  },
  {
    title: '权益状态',
    dataIndex: 'vipcardServerState',
    width: 150,
    dictData: [
      { label: '启用', value: 1 },
      { label: '禁用', value: 0 },
    ],
    renderType: RenderType.DICT,
  },
  {
    title: '数值类型',
    dataIndex: 'numType',
    width: 100,
    customRender: ({ record }: Recordable) => {
      const numTypes = [
        { label: '次', value: '1', color: 'green' },
        { label: '金额', value: '2', color: 'red' },
      ];
      const numType: any = numTypes.find(item => item.value == record.numType);
      if (numType) {
        return h(Tag, { color: numType.color }, () => numType.label);
      }
      return '';
    },
  },
  {
    title: '参数值',
    dataIndex: 'serverValue',
    width: 100,
  },
  {
    title: '是否终身有效',
    dataIndex: 'validDayType',
    width: 100,
    customRender: ({ record }: Recordable) => {
      const validDayTypes = [
        { label: '终身有效', value: '1', color: 'green' },
        { label: '非终身有效', value: '2', color: 'red' },
      ];
      const validDayType: any = validDayTypes.find(item => item.value == record.validDayType);
      if (validDayType) {
        return h(Tag, { color: validDayType.color }, () => validDayType.label);
      }
      return '';
    },
  },
  {
    title: '有效期(天)',
    dataIndex: 'validDay',
    width: 100,
  },
  {
    title: '排序',
    dataIndex: 'serverSort',
    width: 100,
    align: 'center',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'serverName',
    label: '权益名称',
    component: 'Input',
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'divider1',
    label: '基础信息',
    component: 'Divider',
    helpMessage: '基础信息',
  },
  {
    label: '主键',
    field: 'vipcardServerId',
    component: 'Input',
    required: true,
    show: false,
  },
  {
    label: '卡种Id',
    field: 'vipcardId',
    component: 'Input',
    required: true,
    dynamicDisabled: true,
    show: false,
  },
  {
    label: '服务',
    field: 'serverId',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: serverList,
      labelField: 'serverName',
      valueField: 'serverId',
      immediate: true,
    },
    dynamicDisabled: ({ model }) => {
      if (model.vipcardServerId) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    field: 'divider2',
    label: '权益信息',
    component: 'Divider',
    helpMessage: '权益信息',
  },
  {
    label: '数值类型',
    field: 'numType',
    component: 'Select',
    required: true,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '次', value: 1 },
        { label: '金额', value: 2 },
      ],
    },
  },
  {
    label: '参数值',
    field: 'serverValue',
    component: 'InputNumber',
    helpMessage: '期间且数值为0代表不限次',
    required: true,
    componentProps: {
      style: { width: '550px' },
      min: 0,
      addonAfter: '(贵宾服务)计费方式为期间，且数值为0代表不限次',
    },
    dynamicRules: () => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            if (!value && value != 0) {
              return Promise.reject('请输入数字!');
            }
            if (!new RegExp(/^\d+$/).test(value)) {
              return Promise.reject('数字格式有误');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ];
    },
  },
  {
    label: '是否终身',
    field: 'validDayType',
    component: 'Select',
    required: true,
    defaultValue: 2,
    componentProps: ({ formActionType }) => {
      return {
        options: [
          { label: '终身有效', value: 1 },
          { label: '非终身有效', value: 2 },
        ],
      };
    },
  },
  {
    label: '有效期',
    field: 'validDay',
    component: 'Input',
    required: true,
    ifShow: ({ model }) => {
      if (model.validDayType == 2) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '有效截止日期',
    field: 'expirationDate',
    component: 'DatePicker',
    required: false,
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: 'YYYY-MM-DD',
    },
  },
  {
    label: '计费方式',
    field: 'billingWay',
    component: 'Select',
    required: true,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '按次计费', value: 2 },
        { label: '按期间计费', value: 1 },
      ],
    },
  },
  {
    label: '排序',
    field: 'serverSort',
    component: 'InputNumber',
    required: true,
  },
  {
    label: '服务描述',
    field: 'serverDesc',
    component: 'TyEditor',
    required: true,
  },
];
